﻿#include "CAlarmPoint.h"
CAlarmPoint::CAlarmPoint(const QString& sId, const QString& sName, QObject* parent, EPointType type,
	const QString& unit, double LLThreshold, double LThreshold,
	double HThreshold, double HHThreshold)
	: CMeasurePoint(sId, sName, "",parent,type, unit),
	m_dLLThreshold(LLThreshold),
	m_dLThreshold(LThreshold),
	m_dHThreshold(HThreshold),
	m_dHHThreshold(HHThreshold),
	m_eAlarmLevel(EAlarmLevel::enNormal),
	m_eConfirmStatus(EAlarmConfirmStatus::enNormal),
	m_bAlarming(false),
	m_eAlarmType(EAlarmType::enProcess) // 默认是工艺报警
{
	m_bAlarmPoint = true;
	m_bAlarmValue = false;
	m_bAlarmChange = false;
	// 默认区域为空
	m_sAlarmRegion = "";
	m_sAlarmDesc = "";
}

void CAlarmPoint::updateData()
{
	for (CBaseNode *pNode : m_pVecBaseNode)
	{
		pNode->updateData();
	}
}

void CAlarmPoint::setAlarmRegion(const QString& region)
{

}

QString CAlarmPoint::getAlarmRegion() const
{
	return m_sAlarmRegion;
}

void CAlarmPoint::setAlarmType(EAlarmType type)
{
	m_eAlarmType = type;
}

EAlarmType CAlarmPoint::getAlarmType() const
{
	return m_eAlarmType;
}

void CAlarmPoint::setThresholds(double LLThreshold, double LThreshold, double HThreshold, double HHThreshold)
{
	m_dLLThreshold = LLThreshold;
	m_dLThreshold = LThreshold;
	m_dHThreshold = HThreshold;
	m_dHHThreshold = HHThreshold;
}

void CAlarmPoint::setAlarmValue(bool bValue)
{
	m_bAlarmValue = bValue;
}

void CAlarmPoint::getThresholds(double &LLThreshold, double &LThreshold, double &HThreshold, double &HHThreshold) const
{
	LLThreshold = m_dLLThreshold;
	LThreshold = m_dLThreshold;
	HThreshold = m_dHThreshold;
	HHThreshold = m_dHHThreshold;
}

void CAlarmPoint::setAlarmConfirmStatus(EAlarmConfirmStatus status)
{
	QWriteLocker locker(&m_rwLock);
	if (status == EAlarmConfirmStatus::enAlarmPending)
	{
		m_alarmTime= QDateTime::currentDateTime();
		m_eAlarmLevel = EAlarmLevel::enLow;
	}
	else if (status == EAlarmConfirmStatus::enAlarmConfirmed)
	{
		m_eAlarmLevel = EAlarmLevel::enLow;
	}
	else
	{
		m_eAlarmLevel = EAlarmLevel::enNormal;
	}
	m_eConfirmStatus = status;
}

EAlarmConfirmStatus CAlarmPoint::getAlarmConfirmStatus()
{
	return m_eConfirmStatus;
}


void CAlarmPoint::setAlarmStatus(bool bAlarm)
{
	m_bAlarming = bAlarm;
}

void CAlarmPoint::setAlarmConfirm()
{
	if (m_eConfirmStatus == EAlarmConfirmStatus::enAlarmPending)
	{
		m_bAlarmChange = true;
		m_eConfirmStatus = EAlarmConfirmStatus::enAlarmConfirmed;
	}
	else if (m_eConfirmStatus == EAlarmConfirmStatus::enAlarmClearPending)
	{
		m_bAlarmChange = true;
		m_eConfirmStatus = EAlarmConfirmStatus::enNormal;
	}
}

EAlarmConfirmStatus CAlarmPoint::getAlarmConfirmStatus() const
{
	return m_eConfirmStatus;
}

EAlarmLevel CAlarmPoint::getAlarmLevel() const
{
	return m_eAlarmLevel;
}

QDateTime CAlarmPoint::getAlarmTime() const
{
	QReadLocker locker(&m_rwLock);
	return m_alarmTime;
}

void CAlarmPoint::setAlarmStateChange(bool bState)
{
	m_bAlarmChange = bState;
}

bool CAlarmPoint::getAlarmStateChange() const
{
	return m_bAlarmChange;
}

bool CAlarmPoint::isAlarming() const
{
	return m_bAlarming;
}

void CAlarmPoint::setValue(const QVariant& value)
{
	CMeasurePoint::setValue(value);
	checkAlarmCondition(value);
}

void CAlarmPoint::setValue(const QVariant & value, const QString & fileRef)
{
	CMeasurePoint::setValue(value, fileRef);
	checkAlarmCondition(value);
}

void CAlarmPoint::setAlarmDesc(QString sDesc)
{
	QWriteLocker locker(&m_rwLock);
	m_sAlarmDesc = sDesc;
}

QString CAlarmPoint::getAlarmDesc() const
{
	QWriteLocker locker(&m_rwLock);
	return m_sAlarmDesc;
}


void CAlarmPoint::showAlarmUi()
{

}

void CAlarmPoint::checkAlarmCondition(const QVariant& value)
{
	bool wasAlarming = m_bAlarming;
	double dVal = 0;
	switch (m_eType)
	{
	case EPointType::enAnalog:
	{
		dVal = value.toDouble();

		// 根据阈值判断报警状态
		if (dVal < m_dLLThreshold) {
			m_bAlarming = true;
			m_eAlarmLevel = EAlarmLevel::enLowLow;
		}
		else if (dVal >= m_dLLThreshold && dVal < m_dLThreshold) {
			m_bAlarming = true;
			m_eAlarmLevel = EAlarmLevel::enLow;
		}
		else if (dVal >= m_dLThreshold && dVal < m_dHThreshold) {
			m_bAlarming = false;
			m_eAlarmLevel = EAlarmLevel::enNormal;
		}
		else if (dVal >= m_dHThreshold && dVal < m_dHHThreshold) {
			m_bAlarming = true;
			m_eAlarmLevel = EAlarmLevel::enHigh;
		}
		else if (dVal >= m_dHHThreshold) {
			m_bAlarming = true;
			m_eAlarmLevel = EAlarmLevel::enHighHigh;
		}
		break;
	}
	case EPointType::enDigital:
	{
		// 对于数字量类型，值应该是 true 或 false
		if (value.type() == QVariant::Bool)
		{
			if (m_bAlarmValue == value.toBool())
			{
				m_eAlarmLevel = EAlarmLevel::enLow;
				m_bAlarming = true;
			}
			else
			{
				m_eAlarmLevel = EAlarmLevel::enNormal;
				m_bAlarming = false;
			}
		}
		break;
	}	
	default:
		break;
	}
	// 更新报警确认状态  现在和原因状态没变不用处理
	if (m_bAlarming && !wasAlarming) 
	{
		m_eConfirmStatus = EAlarmConfirmStatus::enAlarmPending;
		m_alarmTime = QDateTime::currentDateTime();
		m_bAlarmChange = true;
	}
	else if (!m_bAlarming && wasAlarming)
	{
		if (m_eConfirmStatus == EAlarmConfirmStatus::enAlarmConfirmed)
		{
			m_eConfirmStatus= EAlarmConfirmStatus::enNormal;
			m_bAlarmChange = true;
		}
		else
		{
			m_eConfirmStatus = EAlarmConfirmStatus::enAlarmClearPending;
			m_bAlarmChange = true;
		}
	}	
}
